package a.codetop;

import 其他.DataStructure.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class _10层序 {

    public String serialize(TreeNode root) {
        if(root==null){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        Queue<TreeNode> que = new LinkedList<>();
        //层序题初始rot, 但不用pt，看图
        que.add(root);

        while(!que.isEmpty()) {
            //1234
            TreeNode poll = que.poll();
            sb.append(poll==null?"-1 ":(poll.val+" "));
            if(poll==null){
                continue;
            }
            //不要加if,因为null需要pt第一轮（因为我要还原嘛！）
            que.add(poll.left);
            que.add(poll.right);
        }

        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }

}
